
public class DataGatherer {
	
	// static fields to update
	private static int KEY_WORDS_COUNT[] = new int[10];
	private static int TOTAL_WORDS = 0;
	private static int TOTAL_LINKS = 0;
	private static int TOTAL_PAGES = 0;
	private static long TOTAL_TIME = 0;
	
	private ActualReporter myReporter;
	
	public DataGatherer(ActualReporter theReporter) {
		myReporter = theReporter;
	}
	
	public int getTotalPages() {
		return TOTAL_PAGES;
	}
	
	public void update(String URLname, int[] keyWordCount, int linkCount, int wordCount, long pageDuration) {
		
		// TODO: figure out what to do with pageDuration
		
		updateLinks(linkCount);
		updatePages();
		updateWordCount(wordCount);
		updatePTime(pageDuration);
		updateWords(keyWordCount);
		
		myReporter.report(URLname, TOTAL_PAGES, TOTAL_WORDS, TOTAL_LINKS, KEY_WORDS_COUNT, TOTAL_TIME);
		
	}
	
	public synchronized void updateLinks(int links) {
		TOTAL_LINKS += links;
	}
	
	public synchronized void updatePages() {
		TOTAL_PAGES++;
	}
	
	public synchronized void updateWordCount(int words) {
		TOTAL_WORDS += words;
	}
	
	public synchronized void updateWords(int[] wCount) {
		for (int i = 0; i < wCount.length; i++) {
			KEY_WORDS_COUNT[i] += wCount[i];
		}
	}
	
	public synchronized void updatePTime(long duration) {
		TOTAL_TIME += duration;
	}
	
	
	/*public static String convertStreamToString(java.io.InputStream is) {
	    java.util.Scanner s = new java.util.Scanner(is).useDelimiter("\\A");
	    return s.hasNext() ? s.next() : "";
	}*/

	/* Test Code for DataGatherer.
	 * 
	 * Takes a fixed URL and parses it to count the 
	 * occurrences of the fixed keywords below.
	 * 
	 * Prints the resulting stats.
	 */
//	public static void main(String[] args) throws IOException {
//		int[] update1 = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
//		int[] update2 = {2, 2, 2, 2, 2, 2, 2, 2, 2, 2};
//		int[] update3 = {1, 2, 1, 2, 1, 2, 1, 2, 1, 2};
//		
//		ArrayList<String> tKeys = new ArrayList<String>();
//		tKeys.add("one");
//		tKeys.add("two");
//		tKeys.add("three");
//		tKeys.add("four");
//		tKeys.add("five");
//		tKeys.add("six");
//		tKeys.add("seven");
//		tKeys.add("eight");
//		
//		ActualReporter tRep = new ActualReporter(tKeys, 500);
//		DataGatherer dg = new DataGatherer(tRep);
//		
//		dg.update("www.fake1.com", update1, 10, 500, 10);
//		//System.out.print("Total Words: " + TOTAL_WORDS);
//		//System.out.print(" -- Total Pages: " + TOTAL_PAGES);
//		//System.out.println(" -- Total Links: " + TOTAL_LINKS);
//		
//		dg.update("www.fake2.com", update2, 11, 300, 10);
//		//System.out.print("Total Words: " + TOTAL_WORDS);
//		//System.out.print(" -- Total Pages: " + TOTAL_PAGES);
//		//System.out.println(" -- Total Links: " + TOTAL_LINKS);
//		
//		dg.update("www.fake3.com", update3, 12, 199, 10);
//		//System.out.print("Total Words: " + TOTAL_WORDS);
//		//System.out.print(" -- Total Pages: " + TOTAL_PAGES);
//		//System.out.println(" -- Total Links: " + TOTAL_LINKS);
//		
//
//	}
//	

}
